#ifndef _KLAS_UTILS_HPP_
#define _KLAS_UTILS_HPP_

#include "../include/Vector2.hpp"
#include "../include/Rectangle.hpp"
#include "../include/Timer.hpp"
#include <sstream>
#include <fstream>
using namespace std;

namespace klas
{
	//! Utils contains a bunch of general functionality functions; utilities.
	class Utils
	{
		public:
		
			//! Takes the value of num and returns the next power of two.
      /*!
        \param num The number to get the next power of two from.
        \return The closest power of two to number
      */
	    template <class N>
	    static N getNextPowerOfTwo(N num)
	    {
		    num--;
		    num |= num >> 1;
		    num |= num >> 2;
		    num |= num >> 4;
		    num |= num >> 8;
		    num |= num >> 16;
		    num++;

		    return num;
	    }
	    
	    //! Performs a proper equality test between a and b.
	    static bool isEqual(Real a, Real b);	
	
	    //! Tests to see if rect a intersects with rect b.
	    /*!
	      \param a Rectangle a
	      \param b Rectangle b
	      \return Returns true if rect a is intersecting or inside of rect b
      */
      template <class T>
	    static bool intersects(const Rectangle<T> & a, const Rectangle<T> & b)
	    {
				T   	leftA, leftB,
						  rightA, rightB,
						  topA, topB,
						  bottomA, bottomB;
		
				leftA = a.getX1();
				rightA = a.getX2();
				topA = a.getY1();
				bottomA = a.getY2();
		
				leftB = b.getX1();
				rightB = b.getX2();
				topB = b.getY1();
				bottomB = b.getY2();
		
				if(bottomA <= topB || topA >= bottomB || rightA <= leftB || leftA >= rightB)
					return false;
				
				return true;
			}
			
			
	    //! Tests to see if Vector2 a intersects with rect b.
	    /*!
	      \param a Vector2 a
	      \param b Rectangle b
	      \return Returns true if Vector2 a is intersecting or inside of rect b
      */
      template <class T>
	    static bool intersects(const Vector2<T>& a, const Rectangle<T>& b)
	    {
				return intersects(Rectangle<T>(a, Vector2<T>(1, 1)), b);
			}
			
			//! Gets the current FPS of the system.
			/*! This function needs to be called on a regular basis to generate accurate
					fps results.
					\return The current FPS. */
			static Real getFPS();
			
      //! Returns obj as a string.
      /*! \param obj The object to convert into a string.  << must be overloaded.
          \return A string of obj. */
			template <class T>
			static string getString(const T& obj)
			{
				stringstream ss;
				ss << obj;
				return ss.str();
			}
			
      //! Gets a random value between min and max.
      /*! \return A value between min and max. */
      static Real random(Real min, Real max);

      //! Logs object to a log file.  Appends a newline.
      /*! \param obj The object / message to log.
          \return Success or failure. */
      static bool log(string obj);

      //! Inits utils.
      /*! \param logPath The path of the file to log.
          \return Success or failure. */
      static bool init(string logPath);

		private:
			
			//! Used to update the FPS count
			static Timer m_fpsTimer;
			
			//! The current frame for calculating fps
			static Real  m_fpsFrame,
			
			//! The actual FPS
						m_fps;
			
      static ofstream m_file;
	};
}

#endif
